/**
 * @description  手动入池
 * @author shenhong
 */
import React, { PropTypes, PureComponent, Fragment } from 'react';
import { connect } from 'dva';
import { Form } from '@ant-design/compatible';
import '@ant-design/compatible/assets/index.css';
import { message, Modal } from 'antd';
import { FirstHeaderC, DATA_ENTRY, PrimaryC, SecondHeaderC, AdvancedSearch } from '../../../comps';
import MyModal from '../../investmentResearch/securityMnt/tools/index';
@Form.create()
class EditDetails extends PureComponent {
  constructor(props) {
    super(props);
    this.state = {
      isVisible: false,
      type: 'edit',
    };
  }

  componentDidMount() {
    // this.props.onRef(this);
  }
  // componentWillReceiveProps(nextProps) {

  // }
  // 点击弹出modal框
  handleClick = () => {
    this.setState({
      isVisible: true,
    });
  };
  // 确定
  handelOk = () => {
    const { dispatch, businessKey } = this.props;
    const obj = this.state.query;
    const dataArr = this.child.getData(); // 调用子组件方法获取子组件的值
    if (dataArr !== undefined) {
      if (dataArr.inPoolWay == 'manual') {
        // 手动入池方式执行
        const arr = dataArr;
        arr.type = '0';
        arr.id = businessKey;
        dispatch({
          type: 'couponPoolApprovalM/getAddAndEdit',
          payload: arr,
          callback: (res) => {
            if (res.code == 0) {
              this.props.onOk(true);
              this.setState({ isVisible: false });
            }
          },
        });
      } else if (dataArr.inPoolWay == 'auto') {
        const arr2 = dataArr;
        arr2.type = 'SAVE';
        arr2.id = businessKey;
        dispatch({
          type: 'couponPoolApprovalM/autoAdd',
          payload: arr2,
          callback: (res) => {
            if (res.code == 0) {
              this.props.onOk(true);
              this.setState({
                isVisible: false,
              });
            }
          },
        });
      }
    }
  };

  // 点击取消
  handleCancel = () => {
    this.setState({ isVisible: false });
    this.child.resetDatas();
  };

  // modal框
  showModal = () => {
    const { businessKey, onOk } = this.props;
    const { type, isVisible } = this.state;
    const newProps = { visible: isVisible, title: `编辑`, width: 1300 };
    return (
      <div>
        {isVisible ? (
          <Modal
            {...newProps}
            onCancel={this.handleCancel}
            // className={style.ModalBody}
            onOk={this.handelOk}
          >
            <MyModal
              recordId={businessKey}
              type={type} // 是什么操作（新增，查看，编辑，出池，重新发起）
              onRef={(ref) => (this.child = ref)} // 父组件对本组件的引用
            />
          </Modal>
        ) : null}
      </div>
    );
  };

  render() {
    const { title } = this.props;
    return (
      <div>
        <PrimaryC title={title} onClick={this.handleClick} />
        {this.showModal()}
      </div>
    );
  }
}

export default connect(({ couponPoolApprovalM }) => ({
  couponPoolApprovalM,
}))(EditDetails);
